package net.obxlabs.tweetpad.behaviour;

import net.nexttext.*;
import net.nexttext.behaviour.*;
import net.nexttext.behaviour.control.*;
import net.nexttext.behaviour.standard.*;
import processing.core.*;

public class EdgeCollide extends Condition {
	public static final int LEFT = 0;
	public static final int RIGHT = 1;
	public static final int TOP = 2;
	public static final int BOTTOM = 3;
	
	private int[] bounds;
	private int edge;
	
	public EdgeCollide(PApplet p, int edge, Action trueAction) {
		this(p, edge, trueAction, new DoNothing());
	}
	
	public EdgeCollide(PApplet p, int edge, Action trueAction, Action falseAction) {
		super(trueAction, falseAction);
		
		this.edge = edge;
		bounds = new int[4];
		bounds[LEFT] = 0;
		bounds[RIGHT] = p.width;
		bounds[TOP] = 0;
		bounds[BOTTOM]= p.height;
	}
	
	@Override
	public boolean condition(TextObject to) {
		switch (edge) {
		case LEFT:
			if (to.getBounds().x <= bounds[LEFT]) {
				return true;
			}
			break;
		case RIGHT:
			if ((to.getBounds().x+to.getBounds().width) >= bounds[RIGHT]) {
				return true;
			}
			break;
		case TOP:
			if (to.getBounds().y <= bounds[TOP]) {
				return true;
			}
			break;
		case BOTTOM:
			if ((to.getBounds().y+to.getBounds().height) >= bounds[BOTTOM]) {
				return true;
			}
			break;
		}
		
		return false;
	}

}
